package com.example.mapper;

import com.example.entity.dto.UserDTO;
import com.example.entity.item.*;
import com.example.entity.item.dto.FreeWithItemDTO;
import com.example.entity.market.Market;
import com.example.entity.order.Order;
import com.example.entity.user.Room;
import com.example.entity.user.User;
import com.example.entity.user.UserAddresses;
import com.example.entity.user.mailAccount;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface UserMapper {

    //更新用户信息
    @Update("update user set name=#{name},avatar=#{avatar} where username=#{username}")
    int updateUser(User user);

    //获取用户信息
    @Select("select UID,username,name,role,avatar,address from user where UID=#{UID}")
    UserDTO GetUserInfo(String UID);

    //-----用户注册-----
    @Insert("insert into user(UID,id,username,password,name,avatar,role) values (#{UID},#{id},#{username},#{password},#{name},'https://bu.dusays.com/2024/09/24/66f201fff4183.jpg',#{role})")
    int insert(User user);

    @Insert("insert into mailAccount(id,mailAddress,name,avatar,role,code) values (#{id},#{mailAddress},#{name},#{avatar},#{role},#{code})")
    int email_insert(mailAccount mailaccount);

    @Delete("delete from user where UID=#{UID}")
    int deleteUserById(String UID);

    int updateUserById(User user);

    @Select("select * from user where UID = #{UID}")
    User selectUserById(String UID);

    @Select("select * from item_images where itemId=#{itemId}")
    List<item_images> selectItemUrlById(String itemId);

    @Select("select * from UserAddresses where UID =#{UID}")
    List<UserAddresses> selectUserAddressAll(String UID);

    @Select("select * from items where itemId=#{itemId}")
    Items selectItemById(String itemId);

    @Select("select * from items where itemId=#{itemId}")
    FreeWithItemDTO selectItemByIdWithFree(String itemId);

    @Select("select * from room where roomId=#{roomId}")
    Room selectRoomById(String roomId);

    List<User> selectUserAll(User user);

    @Select("select * from items")
    List<Items> selectItemAll();

    @Select("select * from itemList")
    List<itemList> selectItemListAll();

    @Select("select * from user where username=#{username}")
    User selectUserByUsername(String username);

    @Select("select * from markets")
    List<Market> selectMarket(Market market);

    @Select("select * from markets where marketType='hot'")
    List<Market> selectMarketHot(Market market);

    @Select("select * from mailAccount where mailAddress=#{mailAddress}")
    mailAccount selectByEmailAddress(String mailAddress);

    @Select("select * from carts where UID =#{UID}")
    List<Carts> selectUserCartById(String UID);

    @Select("select * from orders where UID=#{UID}")
    List<Order> selectUserOrderById(String UID);

    @Select("select * from favorites where UID=#{UID}")
    List<Favorites> selectUserFavById(String UID);

    @Select("select * from favorites where UID=#{UID} and itemId=#{itemId}")
    Boolean selectUserFavByIdAndItemId(String UID,String itemId);

    @Update("update mailAccount set code=#{code} where mailAddress=#{mailAddress}")
    int updateEmailCode(String mailAddress,String code);

    @Update("update items set itemStock=#{itemStock} where itemId=#{itemId}")
    int updateItemStock(String itemId,Integer itemStock);

    @Update("update orders set orderStatus=#{state} where trackingNumber=#{trackingNumber}")
    int OrderStatus(String trackingNumber,String state);

    @Insert("insert into carts(itemId,Quantity,UID) values (#{itemId},#{Quantity},#{UID})")
    void JoinCart(Carts carts);

    @Insert("insert into UserAddresses(RecipientName,PhoneNumber,Province,City,District,DetailAddress,UID) values (#{RecipientName},#{PhoneNumber},#{Province},#{City},#{District},#{DetailAddress},#{uid})")
    void AddUserAddress(UserAddresses userAddresses);

    @Delete("delete from carts where cartID=#{cartID}")
    void DeleteCartItem(Integer cartID);

    @Insert("insert into favorites(UID,itemId) values (#{UID},#{itemId})")
    void JoinFav(Favorites favorites);

    @Insert("insert into orders(UID,orderStatus,totalAmount,createdAt,trackingNumber,itemId,Quantity,Address) values (#{UID},#{orderStatus},#{totalAmount},#{createdAt},#{trackingNumber},#{itemId},#{Quantity},#{Address})")
    void SubmitOrder(Order order);

    //分类

    // 根据分类ID动态查询商品
    List<Items> findItemsByCategory(@Param("categoryId") String categoryId,
                                    @Param("typeId") String typeId,
                                    @Param("classId") String classId);
    List<FreeWithItemDTO> findItemsByCategoryWithFree(@Param("categoryId") String categoryId,
                                                      @Param("typeId") String typeId,
                                                      @Param("classId") String classId);

    // 获取所有父级为 null 的根分类
    List<Category> selectRootCategories();

    // 获取某个父分类下的所有子分类
    List<Category> selectSubCategories(String parentId);

    // 根据省份查询市场数据
    List<Market> findMarketsByProvince(@Param("province") String province);

    @Insert("insert into user(UID,username,name,password,avatar,role) values (#{openId},#{openId},#{name},#{openId},#{avatar},'USER')")
    void weChatAutoRegister(String openId, String name, String avatar);
}
